﻿using System.Configuration.Provider;
using System.Web.Configuration;
using OZDoo.Core.Exceptions;
using System.Collections;


namespace OZDoo.Core.Providers
{
    /// <summary>
    /// 提供者管理类
    /// </summary>
    public  class ProviderManage
    {
        /// <summary>
        /// 
        /// </summary>
        //private static Hashtable _sectionName=new Hashtable();
        

        /// <summary>
        /// Public static constructor.
        /// </summary>
        static ProviderManage()
        {
        }

        /// <summary>
        /// Initialization
        /// </summary>
        private static ProviderConfiguration getConfig(string sectionName)
        {
                    OzdConfiguration config = OzdConfiguration.GetConfig();
                    ProviderConfiguration providerConfig =
                        (ProviderConfiguration)config.GetSection(sectionName);
                    if (providerConfig==null) providerConfig = new ProviderConfiguration();
                    return providerConfig;
        }

        /// <summary>
        /// 返回一个提供者实例.
        /// </summary>
        /// <typeparam name="T">泛型，继承自ProviderBase</typeparam>
        /// <param name="section">提供者配置节</param>
        /// <param name="providername">提供者的名称</param>
        /// <returns>返回一个提供者实例</returns>
        public static T getProvider<T>(string section,string providername) where T : ProviderBase
        {
           
            ProviderConfiguration config = getConfig(section);
            if (config.Providers[providername] == null) return null;

            T provider = ProvidersHelper.InstantiateProvider(config.Providers[providername.Trim()], typeof(T)) as T;
            return provider;
        }
        /// <summary>
        /// 返回提供者配置节指定的默认提供者实例
        /// </summary>
        /// <typeparam name="T">泛型,继承自ProviderBase</typeparam>
        /// <param name="section">提供者配置节</param>
        /// <returns>返回一个提供者实例</returns>
        public static T getProvider<T>(string section) where T : ProviderBase
        {
            ProviderConfiguration config = getConfig(section);
            return getProvider<T>(section,config.Default);
        }
        /// <summary>
        /// 返回一个提供者实例
        /// </summary>
        /// <typeparam name="T">泛型,继承自ProviderBase</typeparam>
        /// <param name="config">提供者配置节</param>
        /// <param name="providername">提供者的名称</param>
        /// <returns>返回一个提供者实例</returns>
        public static T getProvider<T>(ProviderConfiguration config, string providername) where T : ProviderBase
        {
            if (config.Providers[providername] == null) return null;
            T provider = ProvidersHelper.InstantiateProvider(config.Providers[providername], typeof(T)) as T;
            if (provider == null) throw new OzdException("provider_create_error");
            return provider;
        }
        /// <summary>
        /// 返回提供者配置节指定的默认提供者实例
        /// </summary>
        /// <param name="config">提供者配置节</param>
        /// <returns>返回一个提供者实例</returns>
        public static ProviderCollection getProviders(ProviderConfiguration config)
        {
            ProviderCollection providers=new ProviderCollection();
            ProvidersHelper.InstantiateProviders(config.Providers,providers,typeof(ProviderBase));
            return providers;
        }
    }
}
